﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBComparer
{
    public partial class SqlServerConnectionWizard : UserControl
    {
        Common.Connection _Connection = null;

        internal Common.Connection Connection
        {
            get { return _Connection; }
            set { _Connection = value; }
        }

        public event EventHandler onConnected;
        public event EventHandler onDataBaseSelectIndexChange;


        public bool buttonEnable { get { return btnConnect.Enabled; } set { btnConnect.Enabled = value; } }

        public string Server { get 
        {
            return txtServer.Text;
        }
            set 
            {
                txtServer.Text = value;
            }
        }

        public SqlServerConnectionWizard()
        {
            InitializeComponent();

            Connection = new Common.Connection();

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (txtServer.Text.Length==0)
            {
                MessageBox.Show("Enter Server Name or IP");
                return;
            }

            ChangeConnectionString("master");

            string _error="";
            
            if (!Connection.Check(out _error))
            {
                MessageBox.Show(_error);
                return;
            }

            cboDatabase.Reload(Connection.GetConnection());

            if (onConnected!=null)
                onConnected(new object(), EventArgs.Empty);

        }

        private void ChangeConnectionString(string argDataBaseName) 
        {
            switch (cboAuthenticationTypeSelect.AuthenticationType)
            {
                case AuthenticationTypeSelect.Type.Windows:
                    Connection.ConnectionString = string.Format("data source={0}; database={1}; Integrated Security=SSPI", txtServer.Text, argDataBaseName);
                    break;
                case AuthenticationTypeSelect.Type.SQL:
                    Connection.ConnectionString = string.Format("data source={0}; database={1}; uid={2}; pwd={3}", txtServer.Text, argDataBaseName, txtUserName.Text, txtPassword.Text);
                    break;
            }
        
        }

        private void SqlServerConnectionWizard_Load(object sender, EventArgs e)
        {

        }

        private void cboDatabase_onDatabaseListLoaded(object sender, EventArgs e)
        {
            cboDatabase.Enabled = true;
        }

        private void cboDatabase_onDatabaseSelectedChange(object sender, EventArgs e)
        {
            ChangeConnectionString(cboDatabase.SelectedDatabase);

            if (onDataBaseSelectIndexChange!=null)
                onDataBaseSelectIndexChange(new object(), EventArgs.Empty);
        }

        private void cboAuthenticationTypeSelect_onTypeChange(object sender, EventArgs e)
        {
            switch (cboAuthenticationTypeSelect.AuthenticationType)
            {
                case AuthenticationTypeSelect.Type.Windows:
                    txtUserName.Enabled = txtPassword.Enabled = false;
                    break;
                case AuthenticationTypeSelect.Type.SQL:
                    txtUserName.Enabled = txtPassword.Enabled = true;
                    break;
                default:
                    break;
            }
        }
    }
}
